//前端发起修改密码
function set(i,c){
    let e = "none";
    if(c) e = "block";
    document.getElementById(i).style.display = e;
    return c
}

function val(i){
    return document.getElementById(i).value;
}

function submit_chpwd() {
    const npwd = val("npwd");
    const opwd = val("opwd");
    const cpwd = val("cpwd");
    let ret = false;
    if (set("chpwd_blank", npwd == "" || opwd == "" || cpwd == "")) ret = true;
    if (set("chpwd_repeat",npwd == opwd && !ret)) ret = true;
    if (set("chpwd_incon",npwd != cpwd && !ret)) ret = true;
    if (set("chpwd_short",npwd.length < 8 && !ret)) ret = true;
    if (ret) return;
    fetch("/auth/chpwd",{
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            "old": opwd,
            "new": npwd
        })
    }).then(res => res.json()).then(data=>{
        if (data.success === true) {
            set("chpwd_ok",true);
        } else {
            console.log(data);
            set("chpwd_fail",true);
        }
    }).catch(err=>{
        set("chpwd_uke",true);
    });
}